package com.marketingexpert.dao.ibatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.marketingexpert.dao.UserDao;
import com.marketingexpert.domain.User;
import com.marketingexpert.domain.permission.UserRole;


public class SqlMapUserDao  extends SqlMapClientDaoSupport  implements UserDao {	

	public int insert(User user)  throws DataAccessException  {
		int id = (Integer)getSqlMapClientTemplate().insert("insertUser", user);
		return id;
	}

	public List<User> queryAllUser(int cursor, int pageSize, String userType) throws DataAccessException {		
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("cursor", cursor);
		params.put("pageSize", pageSize);
		params.put("userType", userType);		
		List<User> users = getSqlMapClientTemplate().queryForList("selectAllUser",params);
		return users;
	}

	public User queryUserByUId(String userId) throws DataAccessException {
		User u = (User) getSqlMapClientTemplate().queryForObject("selectUserByUId",userId);
		return u;
	}

	@Deprecated
	public void setPermission(String userId,String permission) throws DataAccessException {		
		User u = new User();		
		u.setUserId(userId);	
		getSqlMapClientTemplate().update("setPermission", u);		
	}

	public List<User> queryUserByParentId(String parentUserId) throws DataAccessException {
		List<User> users = (List<User>) getSqlMapClientTemplate().queryForList("selectUserByParentId",parentUserId);
		return users;
	}

	public void deleteUserById(String userId) throws DataAccessException {
		try {
			getSqlMapClientTemplate().delete("deleteUserById", userId);
			getSqlMapClientTemplate().delete("deleteUserByParent", userId);
			getSqlMapClientTemplate().delete("deleteTaskByUserId", userId);
			getSqlMapClientTemplate().delete("deleteTabByUserId", userId);	
			getSqlMapClientTemplate().delete("deletePanelByUserId", userId);
			getSqlMapClientTemplate().delete("deleteUserRoleByUserId", userId);
		} catch(DataAccessException e) {
			throw e;
		}		
	}

	public int updateUser(User user) throws DataAccessException {
		try {
			return getSqlMapClientTemplate().update("updateUser", user);
		} catch(DataAccessException e) {
			throw e;
		}		
	}

	@Deprecated
	public void expiredUserById(String userId,String isactive) {
		try {
			User u = new User();
			u.setUserId(userId);
			getSqlMapClientTemplate().update("expiredUserById", u);
		} catch(DataAccessException e) {
			throw e;
		}
	}

	public List<UserRole> queryRoleByUserId(String userId)
			throws DataAccessException {
		try {
			getSqlMapClientTemplate().queryForList("queryRoleByUserId", userId);
			return null;
		} catch(DataAccessException e) {
			throw e;
		}
	}

	public User queryAdmin(String userName) {
		try {
			Map<String,String> params = new HashMap<String, String>();
			return (User)getSqlMapClientTemplate().queryForObject("getAdmin", userName);
		} catch(DataAccessException e) {
			throw e;
		}
	}

	public List<User> queryAllParentUser() {
		
		try {
			return getSqlMapClientTemplate().queryForList("queryAllParentUser");
		} catch(DataAccessException e) {
			throw e;
		}
		
	}

	/*
	 * 获取淘宝用户的总数
	 * @see com.marketingexpert.dao.UserDao#queryUserCount(java.lang.String)
	 */
	@Override
	public int queryUserCount(String userType) {
		try {
			return (Integer)getSqlMapClientTemplate().queryForObject("selectUserCounts",userType);
		} catch(DataAccessException e) {
			throw e;
		}
	}

	@Override
	public User queryUserById(int parentUserId) {
		try {
			return (User)getSqlMapClientTemplate().queryForObject("queryUserById",parentUserId);
		} catch(DataAccessException e) {
			throw e;
		}
	}
	
	
	
}
